﻿using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using AB.ServiceDirectory.BO;
using AB.ServiceDirectory.Commons.DataFilters;

namespace AB.ServiceDirectory.Design.Org
{
    public partial class HOrganizationList : Page
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                var filter = new HOrganizationFilter();
                ViewState["filter"] = filter;
                ViewState["sortingButton"] = LinkButtonAll.UniqueID;
            }
        }

        /// <summary>
        /// Handles the Command event of the FilterByLetters control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void FilterByLetters_Command(object sender, CommandEventArgs e)
        {
            var filter = ViewState["filter"] as HOrganizationFilter;
            if (filter != null)
            {
                Collection<string> beginningLetters = StringTools.GetLetterGroup(e.CommandName);
                filter.SetBeginningLetters(beginningLetters);

                var letterGroupId = ViewState["sortingButton"] as string;
                var letterGroupButton = FindControl(letterGroupId) as LinkButton;

                if (letterGroupButton != null)
                {
                    var clickedButton = sender as LinkButton;
                    if (clickedButton == null)
                    {
                        return;
                    }

                    letterGroupButton.CssClass = string.Empty;
                    clickedButton.CssClass = "hg-blue";
                    ViewState["sortingButton"] = clickedButton.UniqueID;
                }

                GridViewOrg.PageIndex = 0;
                GridViewOrg.DataBind();
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the CheckBoxInactive control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void CheckBoxInactiveCheckedChanged(object sender, EventArgs e)
        {
            var filter = ViewState["filter"] as HOrganizationFilter;
            if (filter != null) filter.IncludeInactive = CheckBoxInactive.Checked;
            GridViewOrg.DataBind();
        }

        /// <summary>
        /// Handles the Selecting event of the ObjectDataSourceOrganisation control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs"/> instance containing the event data.</param>
        protected void ObjectDataSourceOrganizationSelecting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            var filter = ViewState["filter"] as HOrganizationFilter;
            e.InputParameters["filter"] = filter;
        }

        /// <summary>
        /// Handles the Command event of the LinkButtonDepartmentName control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void LinkButtonDepartmentName_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandArgument.Equals("False"))
            {
                int organisationId = int.Parse(e.CommandName, CultureInfo.CurrentCulture);
                OrganizationBO.SetOrganizationStatus(organisationId, true);
            }

            Response.Redirect(string.Format(CultureInfo.CurrentCulture, "~/Organisation/OrganisationDetails.aspx?id={0}",
                                            e.CommandName));
        }
    }
}